/**
 * Handle ticket purchase
 */
package cult.ticketing.processor;

import cult.ticketing.bean.ScheduleItem;
import java.util.Calendar;

import cult.ticketing.dao.PurchaseRecordDAO;
import cult.ticketing.dao.ScheduleItemDAO;
import java.text.DateFormat;

/**
 * @author 51158282
 *
 */
public class PurchaseProcessor {
	private int memberId;
	private String role;
	private int scheduleItemId;
	private final int BOOKED_SEAT = 1;
	private final int NO_REFUND_REQUEST = -1;
	private NotificationProcessor notice; 
	private int totalCost;
	private Calendar purchaseTime;
	private int[] availableSeat;
	private final int NON_MEMBER_ID = -1;
        private String email;
	
	/**
	 * 
	 */
	public PurchaseProcessor(int scheduleItemId) {
		this.scheduleItemId = scheduleItemId;
	}
	
	public PurchaseProcessor(int memberId, int scheduleItemId, String email) {
		this.memberId = memberId;
		this.scheduleItemId = scheduleItemId;
                this.email = email;
	}
	
	public boolean purchaseTicket(String seatIndex) {
            //8,9;7,2
            boolean ifSuccessful = true;

            ScheduleItemDAO scheduleItemDAO = new ScheduleItemDAO();

            if (!scheduleItemDAO.updateBookedSeat(scheduleItemId, seatIndex)){
                ifSuccessful = false;
            }
            else {
                Calendar purchaseTimeCalendar = Calendar.getInstance(); // Get current time
                String purchaseTime = DateFormat.getDateTimeInstance().format(purchaseTimeCalendar.getTime());

                PurchaseRecordDAO purchaseRecordDAO = new PurchaseRecordDAO();
                if (!purchaseRecordDAO.addPurchaseRecord(memberId, email, scheduleItemId, seatIndex, purchaseTime))
                    ifSuccessful = false;
            }

            return ifSuccessful;
	}
	
	/**
	 * Calculate loyalty point by cost
	 * @param totalCost
	 * @return loyalty point value
	 */
	private int calculateLoyaltyPoint(int totalCost) {
		int loyaltyPoint = 0; //loyaltyPoint = cost ..... TODO certain logic
		return loyaltyPoint;
	}

	/**
	 * Calculate total cost of the purchase
	 * Use (new PurchaseProcessor(id)).getTotalCost() to retrive it
	 */
	public int calculatePurchase(String seatIndex) {
	    int ticketCount = seatIndex.split(";").length;

            ScheduleItemDAO scheduleItemDAO = new ScheduleItemDAO();
            ScheduleItem scheduleItem = scheduleItemDAO.getScheduleItemById(scheduleItemId);
            return scheduleItem.getPrice() * ticketCount;
	}

	/**
	 * TODO search how to do timer
	 * Hold the seat for 5 min
	 */
	
	private void timer() {
		
	}
	
	public int getTotalCose() {
		return this.totalCost;
	}
	
	/**
	 * On successful payment ,call this method
	 * @return

	public boolean addPurchaseRecord() {
		boolean ifAddSuccessful = false;
		for (int i=0; i<availableSeat.length; i++)
			PurchaseRecord.addPurchaseRecord(memberId, scheduleItemId, availableSeat[i], NO_REFUND_REQUEST, purchaseTime);
		return ifAddSuccessful;
	}
	
	 */
}
